package cn.pconline.autoclub.util.ubb.node;

import org.apache.commons.lang.StringUtils;

import cn.pconline.autoclub.util.ubb.EscapeUtils;
import cn.pconline.autoclub.util.ubb.Node;

/**
 *
 * @author xhchen
 */
public class ImgNode extends AbstractNode {

    private static String DEFAULT_IMG = "http://www1.pconline.com.cn/2011/images/0810/bbsLoading.jpg";

    public ImgNode() {
    }

    private ImgNode(String name, String attribute) {
        super.name = name;
        super.attribute = attribute;
    }

    @Override
    public StringBuilder getText(java.util.LinkedList<Node> stack) {
        StringBuilder sb = new StringBuilder();
        String url = null;
        String width = null;
        String height = null;
        String needStr = "http://"; // 需要插入的字符串
		if (!StringUtils.startsWithIgnoreCase(text.toString(), needStr)) {
			text.insert(0, needStr); // 如果不包含http:// 就强制性增加。
		}
        text = new StringBuilder(text.toString().replaceAll("<[ ]*/?[ ]*[Ss][Cc][Rr][Ii][Pp][Tt]", ""));

        if (attribute != null) {
            attribute = attribute.replaceAll("<[ ]*/?[ ]*[Ss][Cc][Rr][Ii][Pp][Tt]", "");
            if (attribute.length() > 10 && attribute.charAt(0) == 'h') {
                url = attribute;
            } else {
                String[] wh = attribute.split("[x|\\,]");
                if (wh.length == 2) {
                    width = wh[0];
                    height = wh[1];
                }
            }
        }

        if (url != null) {
            sb.append("<a href=\"");
            EscapeUtils.appendAttribute(url, sb);
            sb.append("\" target=\"_blank\">");
            sb.append("<img name=\"LazyloadImg\" border=\"0\"");
            sb.append(" onload=\"aw_(this)\" onmouseenter=\"aw_(this)\"");
            if (floor == 1 && sequence < 5) {
                sb.append(" src=\"").append(text).append("\"/>");
            } else {
                sb.append(" src=\"").append(DEFAULT_IMG).append("\"");
                sb.append(" src2=\"").append(text).append("\"/>");
            }
            sb.append("</a>");
        } else {
            sb.append("<img name=\"LazyloadImg\" ");

            if (stack != null && stack.peek().getName().equalsIgnoreCase("url")) {
                // inside [url]
                sb.append(" border=\"0\"");
            } else {
                // outside [url]
                if (postId > 0) {
                    sb.append(" class=\"img_link\" onclick=\"vpPhotos_(this,").append(postId).append(",").append(sequence).append(")\"");
                } else {
                    sb.append(" class=\"img_link\" onclick=\"vp_(this)\"");
                }
            }

            if (width != null) {
                sb.append(" style=\"width:").append(width).append("px;");
                sb.append("height:").append(height).append("px;\"");
            }

            sb.append(" onload=\"aw_(this)\" onmouseenter=\"aw_(this)\"");
            if (floor == 1 && sequence < 5) {
                sb.append(" src=\"").append(text).append("\"/>");
            } else {
                sb.append(" src=\"").append(DEFAULT_IMG).append("\"");
                sb.append(" src2=\"").append(text).append("\"/>");
            }
        }

        return sb;
    }

    @Override
    public void appendText(CharSequence text) {
        if (attribute == null) {
            EscapeUtils.appendAttribute(text, super.text);
        } else {
            EscapeUtils.appendHtml(text, super.text);
        }
    }

    @Override
    public Node copyWithoutText() {
        return new ImgNode(name, attribute);
    }

    @Override
    public Node build(String name, String attribute) {
        return new ImgNode(name, attribute);
    }
}
